using System;
using System.ComponentModel;
using System.Transactions;

using SIS.ENT;
using SIS.DAL;


namespace SIS.BLL
{

    /// <summary>
    /// The CourseDetailTableManager class is responsible for managing BO.CourseDetailTable objects in the system.
    /// </summary>
    [DataObjectAttribute()]
    public class CourseDetailTableManager
    {

        #region "Public Methods"

        /// <summary>
        /// Gets a list with all CourseDetailTable objects in the database.
        /// </summary>
        /// <returns>A list with all CourseDetailTable from the database when the database contains any, or null otherwise.</returns>
        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public static CourseDetailTableList GetList()
        {
            return CourseDetailTableDB.GetList();
        }

        /// <summary>
        /// Gets a single CourseDetailTable from the database without its data.
        /// </summary>
        /// <param name="courseDetailKey">The CourseDetailKey of the CourseDetailTable in the database.</param>
        /// <returns>A CourseDetailTable object when the CourseDetailKey exists in the database, or <see langword="null"/> otherwise.</returns>
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static CourseDetailTable GetItem(int courseDetailKey)
        {
            return GetItem(courseDetailKey, false);
        }

        /// <summary>
        /// Gets a single CourseDetailTable from the database.
        /// </summary>
        /// <param name="courseDetailKey">The CourseDetailKey of the CourseDetailTable in the database.</param>
        /// <param name="getCourseDetailTableRecords">Determines whether to load all associated CourseDetailTable records as well.</param>
        /// <returns>
        /// A CourseDetailTable object when the CourseDetailKey exists in the database, or <see langword="null"/> otherwise.
        /// </returns>
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static CourseDetailTable GetItem(int courseDetailKey, bool getCourseDetailTableRecords)
        {
            CourseDetailTable myCourseDetailTable = CourseDetailTableDB.GetItem(courseDetailKey);
            if (myCourseDetailTable != null && getCourseDetailTableRecords)
            {
                myCourseDetailTable.timeScheduleTables = TimeScheduleTableDB.GetListByCourseDetailKey(courseDetailKey);
            }
            return myCourseDetailTable;
        }

        /// <summary>
        /// Saves a CourseDetailTable in the database.
        /// </summary>
        /// <param name="myCourseDetailTable">The CourseDetailTable instance to save.</param>
        /// <returns>The new CourseDetailKey if the CourseDetailTable is new in the database or the existing CourseDetailKey when an item was updated.</returns>
        [DataObjectMethod(DataObjectMethodType.Update, true)]
        public static int Save(CourseDetailTable myCourseDetailTable)
        {
            using (TransactionScope myTransactionScope = new TransactionScope())
            {
                int courseDetailTableCourseDetailKey = CourseDetailTableDB.Save(myCourseDetailTable);
                foreach (TimeScheduleTable myTimeScheduleTable in myCourseDetailTable.timeScheduleTables)
                {
                    myTimeScheduleTable.CourseDetailKey = courseDetailTableCourseDetailKey;
                    TimeScheduleTableDB.Save(myTimeScheduleTable);
                }

                //  Assign the CourseDetailTable its new (or existing CourseDetailKey).
                myCourseDetailTable.CourseDetailKey = courseDetailTableCourseDetailKey;

                myTransactionScope.Complete();

                return courseDetailTableCourseDetailKey;
            }
        }

        /// <summary>
        /// Deletes a CourseDetailTable from the database.
        /// </summary>
        /// <param name="myCourseDetailTable">The CourseDetailTable instance to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public static bool Delete(CourseDetailTable myCourseDetailTable)
        {
            return CourseDetailTableDB.Delete(myCourseDetailTable.CourseDetailKey);
        }

        #endregion

    }

}
